﻿using System.Drawing;
using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars;

namespace Com.OpenCode.Framework.DevForm
{
    /// <summary>
    /// </summary>
    public class RibbonSubButtonItemUI : BarButtonItem, IMenuVisible
    {
        private ICommand _Command;
        #region 构造函数

        public RibbonSubButtonItemUI(BuildItemEventArgs args)
        {
            args.Plugin.Owner = this;
            Caption = args.Plugin.Properties.Get("Text").ToString();
            Image image = null;
            if (args.Plugin.Properties.Exist("Image"))
            {
                string imagePath = System.IO.Path.Combine(args.Plugin.Addin.FileInfo.DirectoryName, args.Plugin.Properties.GetString("Image"));
                if (System.IO.File.Exists(imagePath))
                {
                    image = Image.FromFile(imagePath);
                }
            }
            else
            {
                image = IconDoozer.BulidItem(args.Plugin.Addin, args.Plugin.Properties.GetString("ImageId"));
            }
            if (image == null)
            {
                image = DevFormImage.Ribbon16;
            }
            LargeGlyph = image;
            Glyph = image;
            if (args.Plugin.Properties.Exist("Class"))
            {
                _Command = args.Plugin.Addin.Runtime.CreateObject(args.Plugin.Properties.GetString("Class")) as ICommand;
            }
            else
            {
                _Command = ClassDoozer.BulidItem(args.Plugin.Addin, args.Plugin.Properties.GetString("ClassId")) as ICommand;
            }
            (args.Owner as RibbonSubButtonUI).Manager.Items.Add(this);
        }

        #endregion

        public override bool Enabled
        {
            get
            {
                if (_Command != null)
                {
                    return _Command.Enable();
                }
                return false;
            }
            set { base.Enabled = value; }
        }

        protected override void OnClick(BarItemLink link)
        {
            _Command.Run();
        }

        public void CheckVisible()
        {

        }
    }
}